#coding=utf-8
from models import *
import datetime
from django.utils import simplejson  
from RuleInterface import  getAll


def addAS():
    
    try:
        
        asmodel = ASDESCModel()
        asmodel.ASName = "零壹时光一层入口照度"
        asmodel.ASDescription = "零壹时光一层入口照度值"
        asmodel.ASType = "value"
        asmodel.ASURL = "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000054f08cfa?ResourceID=2"
        asmodel.ASMethod = "GET"
        asmodel.ASContent = ""
        asmodel.ASRequestHeader = ""
        asmodel.ASResponseUnit = "°C"
        
        asmodel.save()
    except:
        return False
    return True
    
    


def addASbyJson(jstr):
    try:
        req = simplejson.loads(jstr)
        asmodel = ASDESCModel()
        asmodel.ASName = req['ASName']
        asmodel.ASDescription = req['ASDescription']
        asmodel.ASType = req['ASType']
        asmodel.ASURL = req['ASURL']
        asmodel.ASMethod = req['ASMethod']
        asmodel.ASContent = req['ASContent']
        asmodel.ASRequestHeader = req['ASRequestHeader']
        asmodel.ASResponseUnit = req['ASResponseUnit']
        
        asmodel.save();
    except Exception,ex :
        print Exception,":",ex
        return False
    return True

 
# if(addAS()):
#     print "ok"


# returnjson = u'{ "_id" : { "$oid" : "53c6194f783be004e41d0536"} , "ASName" : "零壹时光一层入口温度" , "ASDescription" : "零壹时光一层入口温度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000054f08cfa?ResourceID=3" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "°C"}'
# asjson1 = u'{ "ASName" : "零壹时光一层入口温度" , "ASDescription" : "零壹时光一层入口温度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000054f08cfa?ResourceID=3" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "°C"}'
# asjson2 = u'{ "ASName" : "零壹时光一层入口照度" , "ASDescription" : "零壹时光一层入口照度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000054f08cfa?ResourceID=2" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "lx"}'
# asjson3 = u"""{ "ASName" : "零壹时光一层入口湿度" , "ASDescription" : "零壹时光一层入口湿度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000054f08cfa?ResourceID=4" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "%"}"""
# asjson4 = u"""{ "ASName" : "零壹时光一层散座区温度" , "ASDescription" : "零壹时光一层散座区温度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000000acfa8d?ResourceID=3" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "°C"}"""
# asjson5 = u"""{ "ASName" : "零壹时光一层散座区照度" , "ASDescription" : "零壹时光一层散座区照度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000000acfa8d?ResourceID=6" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "lx"}"""
# asjson6 = u"""{ "ASName" : "零壹时光一层散座区湿度" , "ASDescription" : "零壹时光一层散座区湿度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000000acfa8d?ResourceID=4" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "%"}"""
# asjson7 = u"""{ "ASName" : "零壹时光二层办公区温度" , "ASDescription" : "零壹时光一层入口温度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000060f188cb?ResourceID=4" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "°C"}"""
# asjson8 = u"""{ "ASName" : "零壹时光二层办公区照度" , "ASDescription" : "零壹时光一层入口照度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000060f188cb?ResourceID=2" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "lx"}"""
# asjson9 = u"""{ "ASName" : "零壹时光二层办公区湿度" , "ASDescription" : "零壹时光一层入口湿度值" , "ASType" : "value" , "ASURL" : "http://www.cloudsensing.cn:9071/WIFPa/ResourceData.xml/0001000060f188cb?ResourceID=5" , "ASMethod" : "GET" , "ASContent" : "" , "ASRequestHeader" : "" , "ASResponseUnit" : "%"}"""


# asm = ASDESCModel.objects[0]
# 
# print asm.to_json()

# 
# 
# 
# addASbyJson(asjson1)
# if(addASbyJson(asjson2)):
#     print "ok 3"
# addASbyJson(asjson3)
# addASbyJson(asjson4)
# addASbyJson(asjson5)
# addASbyJson(asjson6)
# addASbyJson(asjson7)
# addASbyJson(asjson8)
# addASbyJson(asjson9)


def getRule():
    print getAll()[0].to_json()
    
getRule()
